import 'dart:convert';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/HttpUtil.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/model/base_user_info.dart';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'HttpUtil.dart';
import 'config.dart';

// 登录接口
Future login_api(String url, dynamic data) async {
  Response res = await HttpUtil.request(
    url,
    parameters: data,
    method: HttpUtil.PUT,
    onSuccess: (result) async {
      if (result['code'] == 0) {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setString('Authorization', result['data']);
      }
    },
  );
  return jsonDecode(res.data);
}

// 注册接口
Future enroll_api(dynamic data) async {
  Response res = await HttpUtil.request(
    '$backIP/user/enroll',
    parameters: data,
    method: HttpUtil.POST,
    onSuccess: (result) async {
      if (result['code'] == 0) {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setString('Authorization', result['data']);
      }
    },
  );
  return jsonDecode(res.data);
}

// 获取基本用户信息
Future getBasicUserInfo_api() async {
  String token = await getToken();
  if (token == null) {
    return;
  }
  Response res = await HttpUtil.request(
    '$backIP/user/basic',
    method: HttpUtil.GET,
    headers: {'Authorization': token},
    onSuccess: (result) {
      if (result['code'] == 0) {
        user = BaseUserInfo.init(result['data']);
      } else {
        errorToast(result['message']);
      }
    },
  );
  return jsonDecode(res.data);
}

// 获取用户详细信息
Future getDetailUserInfo_api(String url, Map<String, dynamic> data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    url,
    method: HttpUtil.GET,
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 修改密码接口
Future alterPassword_api(String url, dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    url,
    parameters: data,
    method: HttpUtil.PUT,
    headers: {'Authorization': token},
  );
  return res;
}

// 修改用户信息接口
Future alterUserInfo_api(String url, Map<String, dynamic> data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    url,
    parameters: data,
    method: HttpUtil.PUT,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 修改用户头像
Future alterAvatar_api(String url, Map<String, dynamic> data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    url,
    parameters: data,
    method: HttpUtil.PUT,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 获取其他用户基本信息
Future getOtherUserInfo_api(String url, Map<String, dynamic> data) async {
  String token = await getToken();
  Response res = await HttpUtil.get(
    url,
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 获取用户审核列表
Future getUserVerifyList_api() async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    '$backIP/user/verify/list',
    method: HttpUtil.GET,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 提交用户验证材料
Future submitUserVerifyList_api(dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    '$backIP/user/verify',
    method: HttpUtil.POST,
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 获取用户审核具体信息
Future getUserVerifyDetail_api(dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.get(
    '$backIP/user/verify',
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 提交用户审核结果
Future submitUserVerifyResult_api(dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    '$backIP/user/verify',
    parameters: data,
    method: HttpUtil.PUT,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}